home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / CopyBitsSpeedPalette / CopyBitsSpeedPalette.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  12.3 KB  |  433 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        CopyBitsSpeedPalette.c
  3.  
  4.     Contains:    This program demostrates ways to increase copybits speed when using palettes.    
  5.  
  6.     Written by: JW    
  7.  
  8.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  20.                                             for demonstration purposes.
  21.                 7/9/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #include    "CarbonPrefix.h"
  26. #include     <Dialogs.h>
  27. #include    <Resources.h>
  28. #include    <Menus.h>
  29. #include    <Devices.h>
  30. #include    <ToolUtils.h>
  31. //#include     <GestaltEqu.h>
  32. #include    <Gestalt.h>
  33. #include    <Palettes.h>
  34. #include    <QDOffscreen.h>
  35. #include     <Processes.h>
  36. #include    <OSUtils.h>
  37.  
  38. #define Gestalttest        0xA1AD
  39. #define NoTrap            0xA89F
  40.  
  41. #define    appleID            128            
  42. #define    appleMenu        0
  43. #define    aboutMeCommand    1
  44.  
  45. #define    fileID            129
  46. #define    quitCommand     1
  47.  
  48. #define    PictID            128
  49. #define    clutID            150
  50.  
  51. #define    aboutMeDLOG        128
  52. #define    okButton        1
  53.  
  54. #ifndef topLeft
  55. #define topLeft(r)                      (((Point *) &(r))[0])
  56. #endif
  57.  
  58. #ifndef botRight
  59. #define botRight(r)                     (((Point *) &(r))[1])
  60. #endif
  61.  
  62. /*------------------------------------------------------*/
  63. /*    Global Variables.                                    */
  64. /*------------------------------------------------------*/
  65.  
  66. Rect                TotalRect, minRect, WinMinusScroll, InitWindowSize;
  67. WindowPtr            myWindow;
  68. CTabHandle            mycolors;
  69. PaletteHandle        srcPalette;
  70. Boolean                DoneFlag;
  71. MenuHandle            mymenu0, mymenu1;
  72. PicHandle            ThePict;
  73. GWorldPtr            offscreenGWorld;
  74. //enum PointSelector {topLeft, botRight};
  75.  
  76. void draw();
  77. void doCommand(long mResult);
  78. void showAboutMeDialog();
  79. void init();
  80.  
  81.  
  82. /*------------------------------------------------------*/
  83. /*    showAboutMeDialog().                                */
  84. /*------------------------------------------------------*/
  85.  
  86. void showAboutMeDialog()
  87. {
  88.     GrafPtr     savePort;
  89.     DialogPtr    theDialog;
  90.     short        itemHit;
  91.  
  92.     GetPort(&savePort);
  93.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  94.     //SetPort(theDialog);
  95.     SetPortDialogPort(theDialog);
  96.  
  97.     do {
  98.         ModalDialog(nil, &itemHit);
  99.     } while (itemHit != okButton);
  100.  
  101.     //CloseDialog(theDialog);
  102.     DisposeDialog(theDialog);
  103.  
  104.     SetPort(savePort);
  105.     return;
  106. }
  107.  
  108. /*------------------------------------------------------*/
  109. /*    init().                                                */
  110. /*------------------------------------------------------*/
  111.  
  112. void init()
  113. {
  114.     RgnHandle            tempRgn;
  115.     Rect                BaseRect;
  116.     OSErr                err;
  117.     long                QDfeature/*, OSfeature*/;
  118.     GDHandle            SaveGD;
  119.     CGrafPtr            SavePort;
  120.  
  121.     /*    Initialize Managaer.    */
  122.     //InitGraf(&qd.thePort);
  123.     //InitWindows();
  124.     //InitDialogs(nil);
  125.     InitCursor();
  126.     FlushEvents(everyEvent, 0);
  127.     
  128.     /*    Set up menus.    */
  129.     mymenu0 = GetMenu(appleID);
  130.     //AppendResMenu(mymenu0, 'DRVR');
  131.     InsertMenu(mymenu0,0);
  132.     mymenu1 = GetMenu(fileID);
  133.     InsertMenu(mymenu1,0);
  134.     DrawMenuBar();
  135.     DoneFlag = false;
  136.     ThePict = GetPicture(PictID);
  137.     if (ThePict == nil)
  138.         DoneFlag = true;
  139.  
  140.     /*    Use Gestalt to find is QuickDraw is avaiable.    */
  141.     /*if ((GetOSTrapAddress(Gestalttest) != GetOSTrapAddress(NoTrap))) {
  142.         err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  143.         if (err)
  144.             DoneFlag = true;
  145.         err = Gestalt(gestaltSystemVersion, &OSfeature);
  146.         if (err)
  147.             DoneFlag = true;
  148.         if (!DoneFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0605)
  149.             DoneFlag = true;
  150.         }
  151.     else
  152.         DoneFlag = true;*/
  153.     
  154.     err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  155.     if (err != noErr || QDfeature < gestalt32BitQD)
  156.         DoneFlag = true;
  157.  
  158.     /*    Set Rects.    */
  159.     SetRect(&BaseRect, 40, 60, 472, 282);
  160.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-60, 
  161.                 BaseRect.bottom - 80);
  162.     SetRect(&InitWindowSize, WinMinusScroll.left, WinMinusScroll.top, 
  163.                             WinMinusScroll.right, WinMinusScroll.bottom);
  164.     tempRgn = GetGrayRgn();
  165.     HLock ((Handle) tempRgn);
  166.     //TotalRect = (**tempRgn).rgnBBox;
  167.     GetRegionBounds(tempRgn, &TotalRect);
  168.     /*SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  169.                 (**tempRgn).rgnBBox.bottom - 40);*/
  170.     SetRect(&minRect, 80, 80, TotalRect.right - 40, TotalRect.bottom - 40);
  171.     HUnlock ((Handle) tempRgn);
  172.  
  173.     /*    Open window and set up picture.    */
  174.     GetGWorld (&SavePort, &SaveGD);
  175.     mycolors = GetCTable (clutID);
  176.     (*mycolors)->ctFlags |= 0x4000;
  177.  
  178.     myWindow = NewCWindow(nil, &BaseRect, (ConstStr255Param)"", true, zoomDocProc, 
  179.                             (WindowPtr) -1, true, 150);
  180.     SetGWorld((CGrafPtr)myWindow, SaveGD);
  181.     DrawGrowIcon (myWindow);
  182.  
  183.     srcPalette = NewPalette (((**mycolors).ctSize)+1, mycolors,
  184.             pmTolerant + pmExplicit + pmAnimated, 0);
  185.     SetPalette ((WindowPtr) myWindow, srcPalette, true);
  186.     
  187.     GetGWorld (&SavePort, &SaveGD);
  188.     err = NewGWorld (&offscreenGWorld, 8, &InitWindowSize, mycolors, nil, 0);
  189.     if (err)
  190.         Debugger();
  191.     SetGWorld (offscreenGWorld, nil);
  192.     EraseRect (&InitWindowSize);
  193.     DrawPicture (ThePict, &InitWindowSize);
  194.     SetGWorld (SavePort, SaveGD);
  195. }
  196.  
  197. /*------------------------------------------------------*/
  198. /*    doCommand().                                        */
  199. /*------------------------------------------------------*/
  200.  
  201. void doCommand(mResult)
  202.     long    mResult;
  203. {
  204.     int                     theMenu, theItem;
  205.     //Str255                    daName;
  206.     //GrafPtr                 savePort;
  207.  
  208.     theItem = LoWord(mResult);
  209.     theMenu = HiWord(mResult);
  210.     
  211.     switch (theMenu) {
  212.         case appleID:
  213.             if (theItem == aboutMeCommand)
  214.                 showAboutMeDialog();
  215.             else {
  216.                 /*GetMenuItemText(mymenu0, theItem, daName);
  217.                 GetPort(&savePort);
  218.                 (void) OpenDeskAcc((ConstStr255Param)daName);
  219.                 SetPort(savePort);*/
  220.             }
  221.             break;
  222.  
  223.         case fileID:
  224.             switch (theItem) {
  225.                 case quitCommand:
  226.                     DoneFlag = true;
  227.                     break;
  228.                 default:
  229.                     break;
  230.                 }
  231.             break;
  232.     }
  233.     HiliteMenu(0);
  234.     return;
  235. }
  236.  
  237. /*------------------------------------------------------*/
  238. /*    draw.                                                */
  239. /*------------------------------------------------------*/
  240.  
  241. void draw()
  242. {
  243.     RGBColor        black = {0,0,0}, White = {65535,65535,65535};
  244.     long            before, delay;
  245.     //char theString[255];
  246.     GDHandle        screensDevice;
  247.     Rect            area;
  248.         
  249.     RGBForeColor (&black);
  250.     RGBBackColor (&White);
  251.     
  252.     SetPortWindowPort(myWindow);
  253.     
  254.     EraseRect(&WinMinusScroll);
  255.     
  256.     /* This is the only change made to support a faster copybits on one screen.
  257.         ctFlags is still set above. */
  258.     area = WinMinusScroll;
  259.     /*LocalToGlobal(topLeft);
  260.     LocalToGlobal((Point*)botRight);*/
  261.     LocalToGlobal(&topLeft(area));
  262.     LocalToGlobal(&botRight(area));
  263.     screensDevice = GetMaxDevice(&area);
  264.     if (screensDevice != nil)
  265.         //(**(**offscreenGWorld->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  266.         (**(**(GetPortPixMap(offscreenGWorld))).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  267.     
  268.     /*CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  269.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);*/
  270.     CopyBits ((BitMap *) *(GetPortPixMap(offscreenGWorld)), GetPortBitMapForCopyBits(GetWindowPort(myWindow)), 
  271.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  272.     EraseRect(&WinMinusScroll);
  273.     /*CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  274.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);*/
  275.     CopyBits ((BitMap *) *(GetPortPixMap(offscreenGWorld)), GetPortBitMapForCopyBits(GetWindowPort(myWindow)), 
  276.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  277.     EraseRect(&WinMinusScroll);
  278.     /*CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  279.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);*/
  280.     CopyBits ((BitMap *) *(GetPortPixMap(offscreenGWorld)), GetPortBitMapForCopyBits(GetWindowPort(myWindow)), 
  281.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  282.     EraseRect(&WinMinusScroll);
  283.     /*CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  284.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);*/
  285.     CopyBits ((BitMap *) *(GetPortPixMap(offscreenGWorld)), GetPortBitMapForCopyBits(GetWindowPort(myWindow)), 
  286.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  287.     EraseRect(&WinMinusScroll);
  288.     before = TickCount();
  289.     /*CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  290.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);*/
  291.     CopyBits ((BitMap *) *(GetPortPixMap(offscreenGWorld)), GetPortBitMapForCopyBits(GetWindowPort(myWindow)), 
  292.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  293.     delay = TickCount() - before;
  294. /*    DebugStr("\PIt's me!!!");    */
  295. }
  296.  
  297. /*------------------------------------------------------*/
  298. /*    main().                                                */
  299. /*------------------------------------------------------*/
  300.  
  301. main()
  302. {
  303.     char            key;
  304.     Boolean            track;
  305.     long            growResult;
  306.     EventRecord     myEvent;
  307.     WindowPtr        whichWindow;
  308.     int                yieldTime;
  309.     Rect            tempRect1;
  310.  
  311.  
  312.     init();
  313.     yieldTime = 0;
  314.     for ( ;; ) {
  315.         if (DoneFlag)
  316.             ExitToShell();
  317.             
  318.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  319.             switch (myEvent.what) {
  320.                 case mouseDown:
  321.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  322.                         case inSysWindow:
  323.                             //SystemClick(&myEvent, whichWindow);
  324.                             break;
  325.                         case inMenuBar:
  326.                             doCommand(MenuSelect(myEvent.where));
  327.                             break;
  328.                         case inContent:
  329.                             break;
  330.                         case inDrag:
  331.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  332.                             draw();
  333.                             DrawGrowIcon (whichWindow);
  334.                             break;
  335.                         case inGrow:
  336.                             growResult = GrowWindow (whichWindow, myEvent.where,
  337.                                                     &minRect);
  338.                             SizeWindow(whichWindow, LoWord(growResult), 
  339.                                     HiWord(growResult), true);
  340.                             /*EraseRect(&whichWindow->portRect);
  341.                             SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  342.                                     whichWindow->portRect.top, 
  343.                                     whichWindow->portRect.right-20, 
  344.                                     whichWindow->portRect.bottom - 20);*/
  345.                             EraseRect(GetPortBounds(GetWindowPort(whichWindow), &tempRect1));
  346.                             SetRect(&WinMinusScroll, tempRect1.left, 
  347.                                     tempRect1.top, 
  348.                                     tempRect1.right-20, 
  349.                                     tempRect1.bottom - 20);
  350.                             draw();
  351.                             DrawGrowIcon (whichWindow);
  352.                             break;
  353.                         case inGoAway:
  354.                             track = TrackGoAway (whichWindow, myEvent.where);
  355.                             if (track) {
  356.                                 //CloseWindow (whichWindow);
  357.                                 DoneFlag = true;
  358.                                 }
  359.                             break;
  360.                         case inZoomIn:
  361.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  362.                             if (track) {
  363.                                 ZoomWindow (whichWindow, inZoomIn, true);
  364.                                 /*EraseRect(&whichWindow->portRect);
  365.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  366.                                         whichWindow->portRect.top, 
  367.                                         whichWindow->portRect.right-20, 
  368.                                         whichWindow->portRect.bottom - 20);*/
  369.                                 EraseRect(GetPortBounds(GetWindowPort(whichWindow), &tempRect1));
  370.                                 SetRect(&WinMinusScroll, tempRect1.left, 
  371.                                         tempRect1.top, 
  372.                                         tempRect1.right-20, 
  373.                                         tempRect1.bottom - 20);
  374.                                 draw();
  375.                                 DrawGrowIcon (whichWindow);
  376.                                 }
  377.                             break;
  378.                         case inZoomOut:
  379.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  380.                             if (track) {
  381.                                 ZoomWindow (whichWindow, inZoomOut, true);
  382.                                 /*EraseRect(&whichWindow->portRect);
  383.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  384.                                         whichWindow->portRect.top, 
  385.                                         whichWindow->portRect.right-20, 
  386.                                         whichWindow->portRect.bottom - 20);*/
  387.                                 EraseRect(GetPortBounds(GetWindowPort(whichWindow), &tempRect1));
  388.                                 SetRect(&WinMinusScroll, tempRect1.left, 
  389.                                         tempRect1.top, 
  390.                                         tempRect1.right-20, 
  391.                                         tempRect1.bottom - 20);
  392.                                 draw();
  393.                                 DrawGrowIcon (whichWindow);
  394.                                 }
  395.                             break;
  396.                         default:
  397.                             break;
  398.                         }
  399.                     break;
  400.                 case keyDown:
  401.                 case autoKey:
  402.                     key = myEvent.message & charCodeMask;
  403.                     if ( myEvent.modifiers & cmdKey )
  404.                         if ( myEvent.what == keyDown )
  405.                             doCommand(MenuKey(key));
  406.                     break;
  407.                 case updateEvt:
  408.                     if ((WindowPtr) myEvent.message == myWindow) {
  409.                         BeginUpdate((WindowPtr) myWindow);
  410.                         EndUpdate((WindowPtr) myWindow);
  411.                         draw();
  412.                         }
  413.                     break;
  414.                 case diskEvt:
  415.                     break;
  416.                 case activateEvt:
  417.                     break;
  418.                 case 15:
  419.                     if ((myEvent.message << 31) == 0) {
  420.                         yieldTime = 30;
  421.                         }
  422.                     else {
  423.                         yieldTime = 0;
  424.                         //SetPort((WindowPtr) myWindow);
  425.                         SetPortWindowPort(myWindow);
  426.                         }
  427.                     break; 
  428.                 default:
  429.                     break;
  430.                 }
  431.             }
  432.         }
  433. }